package org.example.myleet.p783;

import org.example.myleet.Utils.Assert;
import org.example.myleet.Utils.TreeNode;

import java.util.Objects;

public class Main {
    public static void main(String[] args) {
        Solution1 solution = new Solution1();
        Assert.isTrue(1 == solution.minDiffInBST(parse(new Integer[]{1,0,48,null,null,12,49}, new Index(0), null)));
        Assert.isTrue(1 == solution.minDiffInBST(parse(new Integer[]{4,2,6,1,3}, new Index(0), null)));
        Assert.isTrue(1 == solution.minDiffInBST(parse(new Integer[]{90,69,null,49,89,null,52}, new Index(0), null)));
    }

    private static TreeNode parse(Integer[] input, Index idx, TreeNode node) {
        if (idx.idx >= input.length) {
            return null;
        }
        if (node == null) {
            node = new TreeNode(input[idx.idx]);
            idx.addOne();
        }
        if (idx.idx < input.length && Objects.nonNull(input[idx.idx])) {
            node.left = new TreeNode(input[idx.idx]);
        }
        idx.addOne();
        if (idx.idx < input.length && Objects.nonNull(input[idx.idx])) {
            node.right = new TreeNode(input[idx.idx]);
        }
        idx.addOne();
        if (idx.idx < input.length && node.left != null) {
            node.left = parse(input, idx, node.left);
        }
        if (idx.idx < input.length && node.right != null) {
            node.right = parse(input, idx, node.right);
        }
        return node;
    }

    static class Index {
        int idx;

        public Index(int idx) {
            this.idx = idx;
        }

        public void addOne() {
            ++idx;
        }
    }
}
